<template>
    <div>
        <span>{{count}}</span>
        <button @click="sign">{{ mark ? '已签到' :'签到'}}</button>
    </div>
</template>

<script>
import request from '../util/request';
export default {
    created(){
        this.uid = localStorage.getItem('uid');
        if(this.uid){
            request.get('/api/day',{uid:this.uid}).then(res => {
                console.log(res);
                if(res.code === 1){
                    this.count = res.count;
                }
            })
        }else{
            this.$router.push('/login');
        }
    },
    data(){
        return {
            count:0,
            uid:'',
            mark:localStorage.getItem('mark')  //标识是否签到
        }
    },
    methods:{
        sign(){
            if(!this.mark){
                request.post('/api/sign',{uid:this.uid}).then(res => {
                    if(res.code === 1){
                        //签到
                        localStorage.setItem('mark','签到成功');
                        this.mark = '签到成功';
                        this.count++;
                    }else{
                        console.log("没有签到成功")
                    }
                })
            }else{
                alert("不能重复签到")
            }
        }
    }
}
</script>
